<template>
  <div class="login-view">
    <div class="login-container">
      <h2>客服系统</h2>
      
      <div class="tabs">
        <button 
          :class="{ active: activeTab === 'user' }" 
          @click="activeTab = 'user'"
        >
          客服登录
        </button>
        <button 
          :class="{ active: activeTab === 'customerService' }" 
          @click="activeTab = 'customerService'"
        >
        用户登录
        </button>
      </div>
      
      <div class="form">
        <div class="form-group">
          <label>用户名</label>
          <input v-model="username" type="text" placeholder="请输入用户名" />
        </div>
        
        <div class="form-group" v-if="activeTab === 'user'">
          <label>团队ID</label>
          <input v-model="teamId" type="number" placeholder="请输入团队ID" />
        </div>
        
        <button class="login-btn" @click="handleLogin">登录</button>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue';
import { useRouter } from 'vue-router';
import { useWebSocket } from '../utils/websocket';

const router = useRouter();
const websocket = useWebSocket();

// 表单数据
const username = ref('');
const teamId = ref(1);
const activeTab = ref('user');

// 登录方法
const handleLogin = async () => {
  if (!username.value) {
    alert('请输入用户名');
    return;
  }
  
  if (activeTab.value === 'user' && !teamId.value) {
    alert('请输入团队ID');
    return;
  }
  
  try {
    // 连接WebSocket
    await websocket.connect();
    
    // 登录
    const loginData = {
      username: username.value,
      teamId: activeTab.value === 'user' ? teamId.value : 0
    };
    
    const success = await websocket.login(loginData);
    
    if (success) {
      // 根据角色跳转到不同页面
      if (activeTab.value === 'user') {
        router.push('/user');
      } else {
        router.push('/customer-service');
      }
    }
  } catch (error) {
    console.error('登录失败:', error);
    alert('登录失败，请检查网络连接');
  }
};
</script>

<style scoped>
.login-view {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-color: #f5f5f5;
}

.login-container {
  width: 400px;
  padding: 30px;
  background-color: white;
  border-radius: 8px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
}

h2 {
  text-align: center;
  margin-bottom: 20px;
  color: #333;
}

.tabs {
  display: flex;
  margin-bottom: 20px;
  border-bottom: 1px solid #e0e0e0;
}

.tabs button {
  flex: 1;
  padding: 10px;
  background: none;
  border: none;
  cursor: pointer;
  font-size: 16px;
  color: #666;
  transition: all 0.3s;
}

.tabs button.active {
  color: #1976d2;
  border-bottom: 2px solid #1976d2;
}

.form-group {
  margin-bottom: 15px;
}

.form-group label {
  display: block;
  margin-bottom: 5px;
  font-weight: 500;
  color: #333;
}

.form-group input {
  width: 100%;
  padding: 10px;
  border: 1px solid #ddd;
  border-radius: 4px;
  font-size: 16px;
}

.login-btn {
  width: 100%;
  padding: 12px;
  background-color: #1976d2;
  color: white;
  border: none;
  border-radius: 4px;
  font-size: 16px;
  cursor: pointer;
  transition: background-color 0.3s;
}

.login-btn:hover {
  background-color: #1565c0;
}
</style>